package com.example.musicdemo.ui.fragment

import android.Manifest
import android.content.pm.PackageManager
import android.database.Cursor
import android.view.View
import androidx.core.app.ActivityCompat
import com.example.musicdemo.R
import com.example.musicdemo.adapter.VBangAdapter
import com.example.musicdemo.base.BaseFragment
import com.example.musicdemo.internal.collection.MusicCollection
import com.example.musicdemo.model.AudioBean
import com.example.musicdemo.ui.activity.AudioPlayActivity
import kotlinx.android.synthetic.main.fragment_vbang.*
import org.jetbrains.anko.noButton
import org.jetbrains.anko.support.v4.alert
import org.jetbrains.anko.support.v4.startActivity
import org.jetbrains.anko.yesButton

class VBangFragment : BaseFragment(),MusicCollection.MusicCallbacks {
    private lateinit var mCollection:MusicCollection
    private lateinit var mAdapter: VBangAdapter
    override fun initView(): View? {

        return View.inflate(context, R.layout.fragment_vbang, null)
    }

    override fun initListener() {
        super.initListener()
        mAdapter = VBangAdapter(context,null)
        lvVBang.adapter = mAdapter
        //设置点击事件
        lvVBang.setOnItemClickListener{ adapterView, view, position, l->
            val cursor = mAdapter?.getItem(position) as Cursor
            val list :ArrayList<AudioBean> = AudioBean.getAudioBeans(cursor)
            //位置position
            //跳转到音乐播放界面


            startActivity<AudioPlayActivity>("list" to list,"position" to position)
        }

    }

    override fun initData() {
        //动态权限申请
        handlePermission()
    }

    private fun handlePermission() {
        val permission = Manifest.permission.READ_EXTERNAL_STORAGE
        //查看是否有权限
        val checkSelfPermission = context?.let { ActivityCompat.checkSelfPermission(it, permission) }
        if (checkSelfPermission == PackageManager.PERMISSION_GRANTED) {
            loadSongs()
        }else{
            if (activity?.let { ActivityCompat.shouldShowRequestPermissionRationale(it,permission) } == true){
                alert("我们只会访问音乐文件","温馨提醒"){
                yesButton { myRequestPermission() }
                noButton {  }
                }.show()

            }else{
                myRequestPermission()
            }
        }
    }

    /**
     * 加载音乐
     */
    private fun loadSongs() {
        mCollection = MusicCollection()
        this.activity?.let { mCollection.onCreate(it,this) }
        mCollection.loadMusic()
    }


    /**
     * 接受权限授权结果
     * requestCode 请求码
     * permissions 权限申请数组
     * grantResults 申请之后结果
     */
    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
            loadSongs()
        }
    }


    /**
     * 真正申请权限
     */
    private fun myRequestPermission() {
        val permissions = arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE)
        requestPermissions(permissions,1)
    }

    override fun onMusicLoad(cursor: Cursor?) {

        //这已经回到了主线程
        mAdapter.swapCursor(cursor)

    }

    override fun onMusicReset() {
    }


    override fun onDestroyView() {
        super.onDestroyView()
        mCollection.onDestroy()
    }
}